#include "QDeleteNotesView.h"
#include "NoteListView/QNoteListItemWidget.h"
#include "NoteListView/QNoteThumbnailListItemWidget.h"
#include "QNotesView.h"
#include "QNotebookPickList.h"
#include "NoteListView/QNoteListItemModel.h"
#include "MeegoSupport.h"

QDeleteNotesView::QDeleteNotesView(const QString& notebookName, MApplicationPage *parent)
    : QvernoteWindow(parent->applicationWindow()), m_hEvernote(QvernoteAPI::Instance()), m_notebookName(notebookName), m_iSelectedCount(0)
{
	ui.setupUi(this);

	this->setWindowTitle(notebookName);

	//m_pbDeleteSelected.setIcon(QIcon(":/IconAdd/general_stop.png"));
	//m_lSelectedCount.setText(trUtf8("0 selected"));
	//ui.statusBar->setLayoutDirection(Qt::RightToLeft);
	//QObject::connect(&m_pbDeleteSelected, SIGNAL(clicked()), this, SLOT(deleteSelectedNotes()));
	//QObject::connect(&m_pbCopyNotes, SIGNAL(clicked()), this, SLOT(copySelectedNotes()));
	//QObject::connect(&m_pbMoveNotes, SIGNAL(clicked()), this, SLOT(moveSelectedNotes()));

	QObject::connect(ui.lwDeleteNoteList, SIGNAL(itemClicked(const QModelIndex &)), this, SLOT(setItemMark(const QModelIndex &)));
	setProgressIndicator(true);
}

QDeleteNotesView::~QDeleteNotesView()
{

}

void QDeleteNotesView::loadNotesIntoView()
{
	Note note;

	setProgressIndicator(true);

	ui.lwDeleteNoteList->setItemModel(new QNoteListItemModel());
	ui.lwDeleteNoteList->scrollTo(ui.lwDeleteNoteList->itemModel()->index(0, 0));
	setProgressIndicator(false);
}

void QDeleteNotesView::setItemMark(const QModelIndex & index)
{
        ((QNoteListItemModel*)ui.lwDeleteNoteList->itemModel())->toggleMark(index);
        ui.lwDeleteNoteList->update(ui.lwDeleteNoteList->boundingRect());
}

void QDeleteNotesView::deleteSelectedNotes()
{
        QNoteListItemWidget* currItem;
        QListWidgetItem* listWidgetItem;

	if(m_iSelectedCount == 0 ||
	       MeegoSupport::MessageBox(trUtf8("Confirm"),
	    		   QString(trUtf8("Ready to delete %1 notes?")).arg(m_iSelectedCount),
	    		   M::YesButton|M::NoButton) == M::NoButton)
			return;

	setProgressIndicator(true);
	ui.deleteSelectedAction->setEnabled(false);

	for(int i = 0; i < ui.lwDeleteNoteList->itemModel()->rowCount(); i++)
	{
		QModelIndex nextRowIndex = ui.lwDeleteNoteList->itemModel()->index(i, 0);

		if(dynamic_cast<QNoteListItemModel*>(ui.lwDeleteNoteList->itemModel())->isMarked(nextRowIndex))
		{
			if(m_hEvernote->deleteNote(qobject_cast<QNoteListItemModel*>(ui.lwDeleteNoteList->itemModel())->getNote(nextRowIndex).guid) == false)
			{
				displayError(trUtf8("Unable to delete note"), QString(m_hEvernote->getLastErrorString().c_str()));
			}
			else
			{
				ui.lwDeleteNoteList->itemModel()->removeRow(i);
				subSelectedCount();
			}
		}
	}

	setProgressIndicator(false);
	ui.deleteSelectedAction->setEnabled(true);

	ui.lwDeleteNoteList->itemModel()->removeRows(0, ui.lwDeleteNoteList->itemModel()->rowCount());
	loadNotesIntoView();

	//TODO ((QNotesView*)m_parent)->loadNotesIntoView();
}

void QDeleteNotesView::copySelectedNotes()
{
	QNotebookPickList list(m_notebookName);
	list.loadNotebooks();
	if(list.exec() == MDialog::Rejected)
		return;

	if(m_iSelectedCount == 0 ||
		   MeegoSupport::MessageBox(trUtf8("Confirm"),
				   QString(trUtf8("Ready to copy %1 notes to %2 notebook?")).arg(m_iSelectedCount).arg(list.getSelectedName()),
				   M::YesButton|M::NoButton) == M::NoButton)
			return;

	setProgressIndicator(true);
	ui.deleteSelectedAction->setEnabled(false);
	ui.copyNotesAction->setEnabled(false);
	ui.moveNotesAction->setEnabled(false);

	for(int i = 0; i < ui.lwDeleteNoteList->itemModel()->rowCount(); i++)
	{
		QModelIndex nextRowIndex = ui.lwDeleteNoteList->itemModel()->index(i, 0);

		if(dynamic_cast<QNoteListItemModel*>(ui.lwDeleteNoteList->itemModel())->isMarked(nextRowIndex))
		{
			if(m_hEvernote->copyNote(qobject_cast<QNoteListItemModel*>(ui.lwDeleteNoteList->itemModel())->getNote(nextRowIndex).guid, list.getSelectedGuid().toStdString()) == false)
			{
				displayError(trUtf8("Unable to copy note"), QString(m_hEvernote->getLastErrorString().c_str()));
			}
			else
			{
				ui.lwDeleteNoteList->itemModel()->removeRow(i);
				subSelectedCount();
			}
		}
	}

	setProgressIndicator(false);
	ui.deleteSelectedAction->setEnabled(true);
	ui.copyNotesAction->setEnabled(true);
	ui.moveNotesAction->setEnabled(true);

	ui.lwDeleteNoteList->itemModel()->removeRows(0, ui.lwDeleteNoteList->itemModel()->rowCount());
	loadNotesIntoView();

	//TODO ((QNotesView*)m_parent)->loadNotesIntoView();
}

void QDeleteNotesView::moveSelectedNotes()
{
	QNotebookPickList list(m_notebookName);
	list.loadNotebooks();
	if(list.exec() == MDialog::Rejected)
		return;

	if(m_iSelectedCount == 0 ||
		   MeegoSupport::MessageBox(trUtf8("Confirm"),
				   QString(trUtf8("Ready to move %1 notes to %2 notebook?")).arg(m_iSelectedCount).arg(list.getSelectedName()),
				   M::YesButton|M::NoButton) == M::NoButton)
			return;

	setProgressIndicator(true);
	ui.deleteSelectedAction->setEnabled(false);
	ui.copyNotesAction->setEnabled(false);
	ui.moveNotesAction->setEnabled(false);

	for(int i = 0; i < ui.lwDeleteNoteList->itemModel()->rowCount(); i++)
	{
		QModelIndex nextRowIndex = ui.lwDeleteNoteList->itemModel()->index(i, 0);

		if(dynamic_cast<QNoteListItemModel*>(ui.lwDeleteNoteList->itemModel())->isMarked(nextRowIndex))
		{
			if(m_hEvernote->moveNote(qobject_cast<QNoteListItemModel*>(ui.lwDeleteNoteList->itemModel())->getNote(nextRowIndex).guid, list.getSelectedGuid().toStdString()) == false)
			{
				displayError(trUtf8("Unable to copy note"), QString(m_hEvernote->getLastErrorString().c_str()));
			}
			else
			{
				ui.lwDeleteNoteList->itemModel()->removeRow(i);
				subSelectedCount();
			}
		}
	}

	setProgressIndicator(false);
	ui.deleteSelectedAction->setEnabled(true);
	ui.copyNotesAction->setEnabled(true);
	ui.moveNotesAction->setEnabled(true);

	ui.lwDeleteNoteList->itemModel()->removeRows(0, ui.lwDeleteNoteList->itemModel()->rowCount());
	loadNotesIntoView();

	//TODO ((QNotesView*)m_parent)->loadNotesIntoView();
}
